package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.TransitTimeEntity;
import io.renren.modules.generator.service.TransitTimeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-29 10:08:36
 */
@RestController
@RequestMapping("generator/transittime")
public class TransitTimeController {
    @Autowired
    private TransitTimeService transitTimeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:transittime:list")
    public R list(@RequestParam Map<String, Object> params){

        Query query = new Query(params);

        List<TransitTimeEntity> list = transitTimeService.queryPage(query);

        int total =transitTimeService.queryPageTotal(params);

        PageUtils pageUtils = new PageUtils(list, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
			TransitTimeEntity transitTime = transitTimeService.selectById(id);

        return R.ok().put("transitTime", transitTime);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:transittime:save")
    public R save(@RequestBody TransitTimeEntity transitTime){
			transitTimeService.insert(transitTime);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:transittime:update")
    public R update(@RequestBody TransitTimeEntity transitTime){
			transitTimeService.updateById(transitTime);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:transittime:delete")
    public R delete(@RequestBody Integer[] ids){
			transitTimeService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据行政区域Ids 查询在途时间
     */

    @PostMapping("/queryTimeByFromDtAreaIdsAndToDtAreaIds")
    public R queryTimeByFromDtAreaIdsAndToDtAreaIds(@RequestBody  Map<String,Set<Integer>> params){
        Set<Integer> fromDtAreaIds = params.get("fromDtAreaIds");
        Set<Integer> toDtAreaIds = params.get("toDtAreaIds");

        if(fromDtAreaIds.size() == 0 || toDtAreaIds.size() == 0){
            return R.error("在途时间，城市代码不能为空");
        }
        TransitTimeEntity transitTimeEntity = transitTimeService.queryTimeByFromDtAreaIdsAndToDtAreaIds(new ArrayList<Integer>(fromDtAreaIds) ,new ArrayList<Integer>(toDtAreaIds));

        return R.ok().put("data",transitTimeEntity);
    }






}
